package io.github.wanggit.antrpc.commons.config;

import lombok.Data;

/** 熔断器配置 */
@Data
public class CircuitBreakerConfig {

    private boolean enabled = false;

    /** 失败率超过70%时熔断，默认以最近100次为移动窗口 */
    private int failureRateThreshold = 70;

    /** 半开状态时尝试的次数 */
    private int permittedNumOfCallsInHalfOpen = 1;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public int getFailureRateThreshold() {
        return failureRateThreshold;
    }

    public void setFailureRateThreshold(int failureRateThreshold) {
        this.failureRateThreshold = failureRateThreshold;
    }

    public int getPermittedNumOfCallsInHalfOpen() {
        return permittedNumOfCallsInHalfOpen;
    }

    public void setPermittedNumOfCallsInHalfOpen(int permittedNumOfCallsInHalfOpen) {
        this.permittedNumOfCallsInHalfOpen = permittedNumOfCallsInHalfOpen;
    }

    public void checkSelf() {
        if (failureRateThreshold <= 0
                || permittedNumOfCallsInHalfOpen <= 0
                || permittedNumOfCallsInHalfOpen > 10) {
            throw new IllegalArgumentException(
                    "failureRateThreshold must be greater than 0, permittedNumOfCallsInHalfOpen must be greater than 0 and less than 10.");
        }
    }
}
